package cn.zktr.Personnel.model.service.tantiantian;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.zktr.Personnel.common.email.Mail;
import cn.zktr.Personnel.model.dao.tantiantian.IntentionDaos;
//import cn.zktr.Personnel.model.dao.tantiantian.TngItnDao;
import cn.zktr.Personnel.model.dao.tantiantian.TngPlanDao;
import cn.zktr.Personnel.model.dao.tantiantian.TngPojDao;
import cn.zktr.Personnel.model.dao.tantiantian.TngRetDao;
import cn.zktr.Personnel.model.dao.tantiantian.TngRusDao;
import cn.zktr.Personnel.model.dao.tantiantian.TngXesDao;
import cn.zktr.Personnel.model.dao.tantiantian.TngXksDao;
import cn.zktr.Personnel.model.dao.tanyali.EmaFebDao;
import cn.zktr.Personnel.model.dao.tanyali.EmaTrtsDao;
import cn.zktr.Personnel.model.mappers.tantiantian.PmsStmMapper;
import cn.zktr.Personnel.model.mappers.tantiantian.TngPlanMapper;
import cn.zktr.Personnel.model.mappers.tantiantian.TngProjectMapper;
import cn.zktr.Personnel.model.pojos.PerEmaFeb;
import cn.zktr.Personnel.model.pojos.PerIntention;
import cn.zktr.Personnel.model.pojos.PerPmsEmp;
import cn.zktr.Personnel.model.pojos.PerPmsZCls;
//import cn.zktr.Personnel.model.pojos.PerTngItn;
import cn.zktr.Personnel.model.pojos.PerTngPlan;
import cn.zktr.Personnel.model.pojos.PerTngPojs;
import cn.zktr.Personnel.model.pojos.PerTngRet;
import cn.zktr.Personnel.model.pojos.PerTngRus;
import cn.zktr.Personnel.model.pojos.PerTngXes;
import cn.zktr.Personnel.model.pojos.PerTngXks;
import cn.zktr.Personnel.model.service.zhouyongcong.findService;
import cn.zktr.Personnel.model.uitls.Homapape;
import cn.zktr.Personnel.model.uitls.ZhijiVo;

@Service
@Transactional(rollbackOn=Exception.class)
public class TngPojService {
	/**
	 * 培训项目表
	 */
	@Autowired
	private TngPojDao pojDao;
	@Autowired
	private TngProjectMapper promer;
	@Autowired 
	private TngPlanMapper plans;
	/**
	 * 培训员工表
	 */
	@Autowired
	private TngXesDao xesdao;
	/**
	 * 培训课程表
	 */
	@Autowired
	private TngXksDao xksdao;
	
	/**
	 * 课程表
	 */
	@Autowired
	private TngRusDao rusdao;
	

	@Autowired
	private EmaTrtsDao trtsD;

	@Autowired
	private TngRetDao retD;

	/**
	 * 职员表
	 */
	@Autowired
	private PmsStmMapper stmmer;
	
	/**
	 * 意向表
	 */
	@Autowired
	private IntentionDaos itnDao;
	/**
	 * 培训计划表
	 */
	@Autowired
	private TngPlanDao planDao;
	
	/**
	 * 考试信息反馈表
	 */
	@Autowired
	private EmaFebDao febDao;
	
	/**
	 * 员工
	 */
	@Autowired
	private findService findser;
	/**
	 * 培训计划
	 */
	@Autowired
	private TngPlanService planser;
	/**
	 * @return查询所有根据职级，stmid分组的员工
	 */
	public JSONArray seleProjectYuan() {
		return promer.seleProjectYuan();
	}
	public List<ZhijiVo> selezhiEmp() {
		List<ZhijiVo> zhiji = promer.selezhijiAll();
		for (ZhijiVo zhijiVo : zhiji) {
			zhijiVo.setEmps(promer.selezhiEmp(zhijiVo.getId()));
		}
		return zhiji;
	}
	/**
	 * @return获取培训项目最新值
	 */
	public PerTngPojs selepojslimt() {
		return promer.selepojslimt();
		
	}
	/**
	 * @return获取培训计划最新纪录
	 */
	public PerTngPlan seleplanlimt() {
		return promer.seleplanlimt();
	}
	/**
	 * @return获取培训项目-员工最新纪录
	 */
	public PerTngXes selexeslimt() {
		return promer.selexeslimt();
	}
	/**
	 * @param entity
	 * @return培训项目新增--直接培训 
	 * 计划添加
	 */
	public void  AddTngpojs(
			PerTngPlan plan,
			String planninds,
			String planmaxds,
			HttpSession session){	
		
		//培训计划
		//开始时间--结束时间
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Timestamp ts1 = new Timestamp(format.parse(planninds).getTime());
			plan.setPlannind(ts1);
			Timestamp ts2 = new Timestamp(format.parse(planmaxds).getTime());
			plan.setPlanmaxd(ts2);
		}catch (Exception e) {
			// TODO: handle exception
		}
		//状态
		plan.setPlanstate("0");
		//创建人
		plan.setPerPmsEmp1(findser.findEmpid(Integer.valueOf(session.getAttribute("id")+"")));
		//审批人
		JSONObject o=plans.selePlanid().getJSONObject(0);
		System.err.println("次数"+o.size());
		System.err.println(o.get("EMPID"));
		plan.setPerPmsEmp2(stmmer.seleEmpids(Integer.parseInt(o.get("EMPID").toString())));
		//新增培训计划
		PerTngPlan saves = planDao.save(plan);
		
	
	}
	/**
	 * 新增培训项目
	 */
	public void addPojsy(PerTngPojs pojs,HttpSession session){
		//培训项目
		//创建时间
		pojs.setPojstime(new Timestamp(System.currentTimeMillis()));
		//创建人
		pojs.setPerPmsEmp(findser.findEmpid(Integer.valueOf(session.getAttribute("id")+"")));
		//培训计划
		pojs.setPerTngPlan(promer.seleplanlimt());
		pojDao.save(pojs);
	}
	 /* @param entity
	 * @return培训项目新增--几乎转项目
	 */
	public void AddTngpojs(PerTngPlan plan,PerTngPojs pojs,HttpSession session) {
		
		plan.setPlanstate("3");
		planDao.save(plan);
		//创建时间
		pojs.setPojstime(new Timestamp(System.currentTimeMillis()));
		//创建人
		pojs.setPerPmsEmp(findser.findEmpid(Integer.valueOf(session.getAttribute("id")+"")));
		//培训计划
		pojs.setPerTngPlan(plan);
		pojDao.save(pojs);
	 }
	/**
	 * @param entity
	 * @return培训人员表新增
	 */
	public void AddTngXes(PerTngPojs pojs,PerPmsEmp emp) {	
		PerTngXes xes=new PerTngXes();
		xes.setPerTngPoj(pojs);
		xes.setPerPmsEmp(emp);
		xesdao.save(xes);
		try {
			//new Mail().stats();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * @param entity
	 * @return培训课程表新增
	 */
	public void AddTngXks(PerTngPojs pojs,PerTngRus rus) {
		PerTngXks xks=new PerTngXks();
		xks.setPerTngPoj(pojs);
		xks.setPerTngRus(rus);
		xksdao.save(xks);
	}
	public void addRet(PerTngRet ret) {
		retD.save(ret);
	}
	/**
	 * @param rusid
	 * @return根据课程id查询证书
	 */
	public PerPmsZCls sele_cls_BYrus(Integer rusid) {
		return  promer.sele_cls_BYrus(rusid);
	}
	/**
	 * @param itn新增信息反馈表
	 */
	public void AddTngFeb(PerIntention in,String n) {
		PerEmaFeb feb=new PerEmaFeb();
		feb.setPerIntention(in);
		feb.setFebstat(n);
		febDao.save(feb);
	}
	//查新PerIntention最新信息
	public PerIntention seleitnlimt() {
		return promer.seleitnlimt();
	}
	
	/**feb
	 * @param entity
	 * @return根据课程id查询
	 */
	public Optional<PerTngRus> seleTngrusId(Integer id) {		
		return rusdao.findById(id);
	}
	/**
	 * @param entity
	 * @return查询所有培训项目
	 */
	public JSONArray seleProAll() {		
		return promer.selePrAll();
	}
	public  void AddInt(PerTngXes pojs,PerPmsEmp emp) {
		// TODO Auto-generated method stub
		PerIntention	itn=new PerIntention();
		itn.setPerTngXe(pojs);
		itn.setPerPmsEmp(emp);
		itn.setIonrem("培训");
		itn.setIonstat(2);
		itnDao.save(itn);
	}
	public Map<String,Object> pojs_yuangong(Integer pojsid,Integer page,Integer rows) {
		Map<String,Object> map=new HashMap<>();
		map.put("total", promer.pojs_yuangong(pojsid).size());
		JSONArray a = promer.pojs_yuangong(pojsid);
		map.put("rows",a);
		return map;
	}
	public Map<String, Object> pojs_kecheng(Integer pojsid, Integer page, Integer rows) {
		Map<String,Object> map=new HashMap<>();
		map.put("total", promer.pojs_kecheng(pojsid).size());
		JSONArray a = promer.pojs_kecheng(pojsid);
		map.put("rows",a);
		return map;
	}
	//
	/**
	 * @param ionid
	 * @param trtsid根据考试结果查询培训结果
	 * 
	 */
	public void fanhuijieguo(Integer ionid,Integer trtsid) {
		PerTngRet ret = promer.sele_xiangmu_yixiang(ionid);//关联结果表，意向表，培训结果，找到意向表对应的员工--培训id
		PerTngRet ret1 = retD.findById(ret.getRetid()).get();
		ret1.setPerEmaTrt(trtsD.findById(trtsid).get());//根据考试结果id找到考试结果数据，拿到对象
		retD.save(ret1);//把结果表新增到自己培训结果表中
	}
	//查询培训所有培训结果返回到页面
	public JSONArray sele_ret(){
		return promer.seleAllxiangmuJieguo();	 
	}
	/**
	 * @param empid
	 * @return查询个人项目结果
	 */
	public Map<String, Object> xiangmuJieguo_Byempid(Integer page,Integer rows,HttpSession session) {
		Map<String,Object> map=new HashMap<>();
		map.put("total",promer.xiangmuJieguo_Byempid(Integer.valueOf(session.getAttribute("id")+"")).size());
		Page<Object> p=PageHelper.startPage(page,rows);
		JSONArray seleProjectYuan =promer.xiangmuJieguo_Byempid(Integer.valueOf(session.getAttribute("id")+""));
		map.put("rows",seleProjectYuan);
		return map;
		
		
	}
	//培训通过
	public void tongguo(Integer retid) {
		PerTngRet perTngRet = retD.findById(retid).get();
		perTngRet.setRetrem("1");
		retD.save(perTngRet);
    }
	//培训不通过
	public void rus_butongguo(Integer retid) {
		PerTngRet perTngRet = retD.findById(retid).get();
		perTngRet.setRetrem("-1");
		retD.save(perTngRet);
    }
	/**
	 * @param empid
	 * @return根据登录员工id查询培训记录
	 */
	public JSONArray gere_peixun(Integer empid) {
		return promer.gere_peixun(empid);
	}
	
	/**
	 * 查询所有培训记录
	 */
	public Map<String, Object> peixunjiluAll(Integer page,Integer rows) {
		Map<String,Object> map=new HashMap<>();
		map.put("total",promer.peixunjiluAll().size());
		Page<Object> p=PageHelper.startPage(page,rows);
		JSONArray seleProjectYuan =promer.peixunjiluAll();
		map.put("rows",seleProjectYuan);
		return map;
		
	}
	public Map<String, Object> peixunjilu_name(String empname) {
		Map<String,Object> map=new HashMap<>();
		map.put("total",promer.peixunjilu_name('%'+empname+'%').size());
	//	Page<Object> p=PageHelper.startPage(page,rows);
		JSONArray seleProjectYuan =promer.peixunjilu_name('%'+empname+'%');
		map.put("rows",seleProjectYuan);
		return map;
		
	}
	public Map<String, Object> TrainingResulttable_cha( String empname) {
		Map<String,Object> map=new HashMap<>();
		map.put("total",promer.TrainingResulttable_cha('%'+empname+'%').size());
		System.err.println(promer.TrainingResulttable_cha('%'+empname+'%').size());
		JSONArray seleProjectYuan =promer.TrainingResulttable_cha('%'+empname+'%');
		map.put("rows",seleProjectYuan);
		return map;
		
	}
	public void fankuixiugai_() throws ParseException {
		JSONArray f = promer.peixunjiluAll_fanhui();
		for (int i = 0; i < f.size(); i++) {
			JSONObject o=(JSONObject) f.get(i);
			//结束时间
			SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
			Date date =sdf.parse(o.get("planmaxd").toString());
			Calendar calendars = Calendar.getInstance();
			calendars.setTime(date);
			int days=calendars.get(Calendar.DATE);
			//系统时间
			Calendar calendar = Calendar.getInstance();           
			int day = calendar.get(Calendar.DATE);
			//系统死时间要大于结束时间
			if(day>days) {
				System.err.println("可以把反馈表的状态修改了");
				PerEmaFeb upde_feb = promer.upde_feb(Integer.parseInt(o.get("xesid").toString()));
				Optional<PerEmaFeb> fe = febDao.findById(upde_feb.getFebid());
				fe.get().setFebstat("1");
				febDao.save(fe.get());
			}else {
				System.err.println("培训还没结束呢，不能修改feb表状态，传数据给考试！");
			}


			
		}
		
	}
	public JSONArray TrainingProjectables_name(String text) {
		
		return promer.TrainingProjectables_name("%"+text+"%");
	}
}
